import wx

class WrappingStaticText(wx.StaticText):
    '''A static text widget that automatically wraps the text to fit'''
    def __init__(self, parent, id=wx.ID_ANY, label="",
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=0, name="wst"):
        super(WrappingStaticText, self).__init__(parent, id=id, label=label, pos=pos, size=size, style=style, name=name)
        # save un-wrapped label in its original form
        # since wrapping inserts actual newlines 
        self._label = label
        self._Rewrap()
        self.Bind(wx.EVT_SIZE, self.OnSize)

    def OnSize(self, evt):
        self._Rewrap()

    def _Rewrap(self):
        self.Freeze()
        self.SetLabel(self._label)
        self.Wrap(self.GetSize().width)
        self.Thaw()

if __name__ == "__main__":

    text = """\
A certain king had a beautiful garden, and in the garden stood a tree which bore golden apples. These apples were always counted, and about the time when they began to grow ripe it was found that every night one of them was gone. '
"""

    class TestFrame(wx.Frame):
        def __init__(self, *args, **kw):
            wx.Frame.__init__(self, *args, **kw)

            p = wx.Panel(self)
            panel = wx.Panel(p)
            sbs = wx.BoxSizer(wx.VERTICAL)
            awst = WrappingStaticText(p, label=text)
            sbs.Add(awst, 1, wx.EXPAND)
            panel.SetSizer(sbs)

            sizer = wx.BoxSizer()
            sizer.Add(sbs, 1, wx.EXPAND|wx.ALL, 25)
            p.SetSizer(sizer)


    app = wx.App(redirect=False)
    frm = TestFrame(None, title="test wrapping static text")
    frm.Show()
    #import wx.lib.inspection
    #wx.lib.inspection.InspectionTool().Show()
    app.MainLoop() 
